<?php
namespace app\api\controller\com;
class Article extends Base {
	//列表
	public function lists() {
		$d = $this->post;
		$rule = [
			'page' => 'require|integer',
			'limit' => 'require|integer',
		];
		$msg = [];
		check($rule, $msg, $d);

		$map = [];
		if ($d['cat_id'] != '') {
			if ($d['type'] == 0) {
				$map[] = ['and', 'a.cat_id', '=', $d['cat_id']];
			} else {
				$ids = [$d['cat_id']];
				//下级分类id,放入$ids
				$map[] = ['and', 'a.cat_id', 'in', $ids];
			}
		}
		if ($d['keyword'] != '') {
			$map[] = ['and', 'a.title', 'like', "%{$d['keyword']}%"];
		}
		$w = where($map);
		$rs = $this->db->query("
          select
            a.id,a.title,a.keyword,a.description,a.pic,a.content,a.views,a.create_time,b.cat_title
          from
            article a
          inner join
            article_cat b on a.cat_id = b.id
          $w[0] order by a.sort desc,a.id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
		foreach ($rs[0] as $k => $v) {
			$rs[0][$k]['h5_url'] = url('h5/articledetail', ['id' => $v['id']]);
		}
		if ($rs) {
			return suc($rs[0], $rs[1]);
		}
		return suc([], 0);
	}

	public function detail() {
		$id = $this->post("id");
		$rs = $this->db->query("select id,title,keyword,description,pic,content,views,create_time from article where id = ?", [$id], 1);
		if ($rs) {
			$rs['h5_url'] = url('h5/articledetail', ['id' => $rs['id']]);
			return suc(['info' => $rs]);
		}
		return suc([], 0);
	}
}
